package com.cdtu.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cdtu.entity.*;
import com.cdtu.service.*;
import com.cdtu.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@RestController
//解决跨域问题
@CrossOrigin
@RequestMapping("/echars")
public class EcharsController {

    @Autowired
    private UserService userService;

    @Autowired
    private CertifyTypeService typeService;

    @Autowired
    CertifyTypeService certifyTypeService;
    @Autowired
    private RUserSpecialityService rUserSpecialityService;

    @Autowired
    private SpecialityService specialityService;
    @Autowired
    SpecialityTypeService specialityTypeService;

    @Autowired
    private RUserTaskService rUserTaskService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private RUserCertifyService rUserCertifyService;

    @Autowired
    private CertifyService certifyService;

    @Autowired
    ResourceService resourceService;

    /**
     * 获取总任务数
     * @author wp
     * @param
     * @date 2023/3/8 10:27
     * @return
     */
    @GetMapping("/allTaskNum")
    public R getAllTaskNum(){
        long count = taskService.count();
        return R.success(count);
    }

    /**
     * 获取已完成的任务数
     * @author wp
     * @param
     * @date 2023/3/8 10:27
     * @return
     */
    @GetMapping("/allCompletedTaskNum")
    public R getAllCompletedTaskNum(){
        List<Task> taskList = taskService.list();
        int count = 0;
        for (Task task : taskList) {
            if ((new Date().after(task.getEndTime()))) {
                count++;
            }
        }
        return R.success(count);
    }
    /**
     * 获取志愿者总人数
     * @author wp
     * @param
     * @date 2023/3/8 10:45
     * @return
     */
    @GetMapping("/userNum")
    public R getAllUserNum(){
        long count = userService.count();
        return R.success(count);
    }
    /**
     * 获取持证志愿者总人数
     * @author wp
     * @param
     * @date 2023/3/8 10:45
     * @return
     */
    @GetMapping("/licensedUserNum")
    public R getAllLicensedUserNum(){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getGrade,'a');
        long count = userService.count(queryWrapper);
        return R.success(count);
    }

    /**
    *  获取志愿者分布
    * @author wp
    * @param
     *
    * @date 2023/4/9 9:37
    * @return
    */
    @GetMapping("userDistribution")
   public R getUserDistribution(){

       List<User> userList = userService.list();
       List<List> collect = new ArrayList<>();
        for (User user : userList) {
            if (user.getLongitude() != null && user.getLatitude() != null) {
                List list = new ArrayList<>();
                list.add(user.getLongitude());
                list.add(user.getLatitude());
                list.add(user.getName());
                list.add(user.getPhone());
                collect.add(list);
            }
        }
        return R.success(collect);
   }


    /**
     * 获取年龄分布比例
     * @author wp
     * @param
     * @date 2023/3/8 13:33
     * @return
     */
    @GetMapping("/ageSegments")
    public R getAgeSegments(){
        int[] array = new int[]{0,0,0};
        List<User> listUser = userService.list();
        if (listUser.size() == 0 ){
            return R.success(array);
        }
       if(listUser.size()>0){
           for (User user : listUser) {
              if (user!=null){
                 if (user.getAge()!=null){
                     if (user.getAge()<30){
                         array[0]++;
                     } else if (user.getAge()<=50) {
                         array[1]++;
                     } else {
                         array[2]++;
                     }
                 }
              }
           }
       }
        int size = listUser.size();
        for (int i=0;i<3;i++){
            array[i] = (array[i]*100)/size;
        }

        return R.success(array);
    }

    /**
     *获取男女性别比例
     * @author wp
     * @param
     * @date 2023/3/8 15:09
     * @return
     */
    @GetMapping("/genderRatio")
    public R getGenderRatio(){
        int countManNum=0;
        int countNum=0;
        List<User> list = userService.list();
        for (User user : list) {
            if (user.getSex() == 1){
                countManNum++;
            } else {
                countNum++;
            }
        }
        Object[] genderRatioArray = new Object[2];
        Map mapMan = new HashMap<>();
        mapMan.put("value",countManNum);
        mapMan.put("name",'男');
        Map map = new HashMap<>();
        map.put("value",countNum);
        map.put("name",'女');
        genderRatioArray[0]= mapMan;
        genderRatioArray[1]= map;
        return R.success(genderRatioArray);
    }
    @GetMapping("/echarts1")
    public R getEChartsData(){
        List<Long> seriesData = userService.findIdAndGrade();
        return R.success(seriesData);
    }

    @GetMapping("/echarts2")
    public R getEChartsData2(){
        List<CertifyType> certifyTypes = typeService.getAllType();
        List<CertifyTypeVO> certifyTypeVOS=new ArrayList<>();

        for (CertifyType certifyType : certifyTypes) {
            CertifyTypeVO certifyTypeVO=new CertifyTypeVO();

            Long tid = certifyType.getId();


//            根据证书类型id查出所有证书id
            List<Long> certifyId = certifyService.findByType(tid);

            //            根据单个证书id查询所有的使用者
            long count= 0l;
            for (Long cid : certifyId) {
                LambdaQueryWrapper<RUserCertify> queryWrapper=new LambdaQueryWrapper<>();
                queryWrapper.eq(RUserCertify::getCertifyId,cid);
                count += rUserCertifyService.count(queryWrapper);
            }

            certifyTypeVO.setValue(count);

            certifyTypeVO.setName(certifyType.getName());

            certifyTypeVOS.add(certifyTypeVO);
        }
        return R.success(certifyTypeVOS);
    }
    /**
    * 获取物质类型
    * @author wp
    * @param
    * @date 2023/3/8 19:08
    * @return
    */
        @GetMapping("/resourceType")
    public  R getResourceTypeName(){
        List resourceTypeList = new ArrayList();
        List<Resource> resourceList = resourceService.list();
        Set<String> resourceTypeSet = new HashSet<>();
        for (Resource resource : resourceList) {
            resourceTypeSet.add(resource.getCategory());
        }
        for (String  item : resourceTypeSet) {
             Map map =new HashMap();
             map.put("value",0);
             map.put("name",item);
             resourceTypeList.add(map);
        }
        return R.success(resourceTypeList);
    }
    /**
    *获取具体某一类物质的名称和数量
    * @author wp
    * @param
    * @date 2023/3/8 19:24
    * @return
    */
    @GetMapping("/resourceDetail")
    public  R getResourceDetail(@RequestParam("typeName") String typeName ){
        List nameList = new ArrayList();
        List numList  = new ArrayList();
        LambdaQueryWrapper<Resource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!StrUtil.hasBlank(typeName),Resource::getCategory,typeName);
        List<Resource> resourceList = resourceService.list(queryWrapper);
        if (resourceList.size()>0){
            for (Resource resource : resourceList) {
                nameList.add(resource.getName());
                numList.add(resource.getStock());
            }
        }
        Map map  = new HashMap();
        map.put("nameList",nameList);
        map.put("numList",numList);
        return R.success(map);
    }
}
